﻿using MediatR;
using W.Warehouse.Api.Application.Command.AllocationCommand;
using W.Warehouse.Api.Application.Command.StorageCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Api.Application.CommandHandlder.StorageCommandHandlder
{
    public class StorageAuditCommandHandler : IRequestHandler<StorageAuditCommand, ApiResult<int>>
    {
        private readonly ILogger<StorageAuditCommandHandler> _logger;
        private readonly IStorageRepository _storageRepository;
        private readonly IStorageProductsRepository _storageProductsRepository;
        public StorageAuditCommandHandler(ILogger<StorageAuditCommandHandler> logger, IStorageRepository storageRepository, IStorageProductsRepository storageProductsRepository)
        {
            _logger = logger;
            _storageRepository = storageRepository;
            _storageProductsRepository = storageProductsRepository;
        }
        public async Task<ApiResult<int>> Handle(StorageAuditCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            try
            {
                var list = await _storageRepository.GetAllAsync();
                var slist = list.Where(c => c.StorageId == request.StorageId).FirstOrDefault();
                if (slist != null)
                {
                    slist.ExaminePeople = "yu";
                    slist.ExamineTime = DateTime.Now;
                    slist.StorageStatus = request.StorageStatus;
                    slist.ExamineReason = request.ExamineReason;
                }
                result.Code = await _storageRepository.UpdateAsync(slist);
                _logger.LogInformation("入库审核功能成功");
            }
            catch (Exception el)
            {
                result.Code = -1;
                result.Message = el.Message;
                _logger.LogError(el, el.Message);
                //throw;
            }
            return result;
        }
    }
}
